﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using nplJobsDAL.Common;

namespace nplJobsDAL.Select
{
    public class SelectUserType : DataAccessBase
    {
        public Guid UserId { get; set; }

        public SelectUserType()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectUserType.ToString();
        }

        #region GetUserTypeByEmail
        public tblUserType GetTblUserType()
        {
            DataBaseHelper dbHelper;
            DataSet dsUserType;

            try
            {
                SelecttblUserTypeParameters selecttblUserTypeParameters = new SelecttblUserTypeParameters(this.UserId);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selecttblUserTypeParameters.Parameters;
                dsUserType = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsUserType);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion


        #region SQL parameters for selecting job seeker language proficiency by jobseeker id
        public class SelecttblUserTypeParameters
        {
            private SqlParameter[] _parameters;

            public SelecttblUserTypeParameters(Guid userId)
            {
                this.UserId = userId;
                Build();
            }
            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@UserID", UserId),
                };

                Parameters = parameters;
            }

            public Guid UserId { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
        #region GetList
        private tblUserType GetList(DataSet dsUserType)
        {
            tblUserType userType;
            try
            {
                userType = (from n in dsUserType.Tables[0].AsEnumerable()
                            select new tblUserType
                            {
                                UserId = n.Field<Guid>("UserId"),
                                UserType = n.Field<int>("UserType"),
                                ID = n.Field<int>("ID")
                            }).FirstOrDefault();

                return userType;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
